package com.maixun.uitrasound.main

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lmoumou.lib_comment.CommentPagerAdapter
import com.maixun.lib_comment.ARouterConstance
import com.maixun.lib_comment.ui.BaseMVPActivity
import com.maixun.uitrasound.R
import com.maixun.uitrasound.entity.HomeTitleBeen
import kotlinx.android.synthetic.main.activity_main.*

@Route(path = ARouterConstance.APP.URL_MAIN)
class MainActivity : BaseMVPActivity<MainPresenterImpl>(), MainContract.View {

    override val mPresenter: MainPresenterImpl by lazy { MainPresenterImpl(this) }

    override fun getLayoutId(): Int = R.layout.activity_main

    private val titleArray by lazy {
        arrayOf(
            HomeTitleBeen("首页", R.drawable.select_home_tb),
            HomeTitleBeen("问答中心", R.drawable.select_college_tb),
            HomeTitleBeen("在线平台", R.drawable.select_knowledge_tb),
            HomeTitleBeen("个人中心", R.drawable.select_mine_tb)
        )
    }

    private val pagerAdapter by lazy {
        CommentPagerAdapter(
            this, mutableListOf(
                ARouter.getInstance().build(ARouterConstance.HOME.URL_HOME_MAIN)
                    .navigation() as Fragment,
                ARouter.getInstance().build(ARouterConstance.COLLEGE.URL_COLLEGE_MAIN)
                    .navigation() as Fragment,
                ARouter.getInstance().build(ARouterConstance.KNOWLEDGE.URL_KNOWLEDGE_MAIN)
                    .navigation() as Fragment,
                ARouter.getInstance().build(ARouterConstance.MINE.URL_MINE_MAIN)
                    .navigation() as Fragment
            )
        )
    }


    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarImmerse(true)

        mViewPager.adapter = pagerAdapter
        mViewPager.offscreenPageLimit = titleArray.size
        mViewPager.isUserInputEnabled = false

        mViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    2 -> {
                        setStatusBarStyle(true)
                    }
                    else -> {
                        setStatusBarStyle(
                            false,
                            ContextCompat.getColor(
                                this@MainActivity,
                                R.color.colorPrimary
                            )
                        )
                    }
                }
            }
        })

        TabLayoutMediatorCopy(mTabLayout, mViewPager,
            TabLayoutMediatorCopy.TabConfigurationStrategy { tab, position ->
                titleArray[position].run {
                    tab.text = title
                    tab.setIcon(icon)
                }
            }).attach()
    }


}